package com.bnq.control.desktop.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.bnq.control.R;
import com.bnq.control.common.utils.DisplayUtils;


/**
 * Created by zou on 2016/11/9.
 */

public class AndroidBall extends View {
    public static final int WITH = 43;
    public static final int HEIGHT = 43;

    private float prex, prey;

    private BallListener ballListener;
    private GestureDetector detector;
    private Bitmap bitmap;

    private RectF dst;


    public AndroidBall(final Context context, final BallListener ballListener) {
        super(context);
        this.ballListener = ballListener;
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.android_ball);
        dst = new RectF(0, 0, DisplayUtils.dp2px(WITH, context), DisplayUtils.dp2px(HEIGHT, context));

        detector = new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (e.getY() <= DisplayUtils.dp2px(HEIGHT, context)) ballListener.onClick();
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }
        });
    }

    public AndroidBall(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AndroidBall(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.drawBitmap(bitmap, null, dst, null);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                prex = event.getRawX();
                prey = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = event.getRawX() - prex;
                float dy = event.getRawY() - prey;
                if (this.ballListener != null) {
                    this.ballListener.onBallMove(dx, dy);
                }
                prex = event.getRawX();
                prey = event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                if (this.ballListener != null) {
                    this.ballListener.toSide();
                }
                break;
        }
        return true;
    }

    public interface BallListener {
        void onBallMove(float dx, float dy);

        void toSide();

        void onClick();
    }
}
